#ifndef __JUMP_DOWN_CONTROLLER_H__
#define __JUMP_DOWN_CONTROLLER_H__

#include <cocos2d.h>
#include <list>
#include <functional>
#include "gofgame/common/cxx_0x.h"
#include "gofgame/jumpdown/io/Page.h"
#include "gofgame/jumpdown/io/Score.h"

using namespace cocos2d;
using namespace std;

namespace gofgame {
	namespace jumpdown {
		namespace io {
			class JumpDownController : public CCObject {

			public:
				JumpDownController();
				void submitScore(const char *userName, int level);
				void queryScores(int pageIndex, int pageSize);
				void dispose();

			public:
				void addSubmitScoreListener(function<void(int*)> listener);
				void addQueryScoreListener(function<void(Page<Score>*)> listener);

			private:
				void sumitScoreCallback(cocos2d::CCNode *sender, void *data);
				void queryScoresCallback(cocos2d::CCNode *sender, void *data);

			private:
				list<function<void(int*)>> sumitScoreListeners;
				list<function<void(Page<Score>*)>> queryScoresListeners;
				bool disposed;
				
			private:
				class SAXDelegatorImpl : public CCSAXDelegator {
				public:
					virtual void startElement(void *ctx, const char *name, const char **atts) _override;
					virtual void endElement(void *ctx, const char *name) _override;
					virtual void textHandler(void *ctx, const char *s, int len) _override;
					Page<Score> buildPage();

				private:
					int pageIndex;
					int pageSize;
					int totalRowCount;
					vector<Score> scores;
				};
			};
		}
	}
}

#endif //__JUMP_DOWN_CONTROLLER_H__
